package com.diandiansong.manager.ui.biz;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;

import com.diandiansong.app.R;
import com.diandiansong.app.utils.Api;
import com.diandiansong.manager.entity.SalesTeamInfoInfo;
import com.diandiansong.manager.entity.SalesUserCustomerInfo;
import com.diandiansong.manager.utils.M;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import diandiansong.library.ui.base.BaseAct;
import diandiansong.library.ui.base.FastListFragment;
import diandiansong.library.utils.BaseEntity;
import diandiansong.library.utils.CCallBack;

/**
 * 团队管理
 */
public class Team extends BaseAct {
    public static void start(Context context) {
        Intent starter = new Intent(context, Team.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitle("团队管理");
        addFragment(new SubFragment());
    }


    public static class SubFragment extends FastListFragment {

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            EventBus.getDefault().register(this);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            EventBus.getDefault().unregister(this);
        }

        @Override
        public void initView(View rootView) {
            super.initView(rootView);
            rootView.findViewById(R.id.go_add).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TeamAddMember.start(getActivity());
                }
            });
        }

        @Override
        protected int getLayoutId() {
            return R.layout.manager_team;
        }

        @Override
        public int getItemLayoutRes() {
            return R.layout.manager_team_mana_item;
        }

        @Override
        public void bindItemView(int position, @NonNull View convertView) {
            final SalesTeamInfoInfo.Data item = (SalesTeamInfoInfo.Data) getItem(position);

            fastText(convertView, R.id.tv_1, item.getNickname());
            fastText(convertView, R.id.tv_2, item.getCard_number());

            fastOnClick(convertView, R.id.do_del, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    M.salesTeamDel(item.getId(), new CCallBack<BaseEntity>() {
                        @Override
                        public void onSuccess(BaseEntity baseEntity) throws Exception {
                            mAdapter.remove(item);
                        }

                        @Override
                        public void onFailure(int code, String msg) {
                            toast(msg);
                        }
                    });
                }
            });
        }

        /**
         * 滑到底部时调用
         */
        @Override
        protected void loadMore() {
            M.salesTeamInfo(mPage, new CCallBack<SalesTeamInfoInfo>() {
                @Override
                public void onSuccess(SalesTeamInfoInfo o) throws Exception {
                    List<SalesTeamInfoInfo.Data> data = o.getData();
                    if (mPage.equals("0")) {
                        mAdapter.setData(data);
                    } else {
                        mAdapter.addAll(data);
                    }
                    if (mAdapter.getDataSize() > 0) {
                        mPage = ((SalesTeamInfoInfo.Data) getLast()).getId();
                    }
                    onLoadFinishHasMore(data.size() >= Api.PAGE_SIZE);
                }

                @Override
                public void onFailure(int code, String msg) {
                    onLoadFinishNoMore();
                    toast(msg);
                }
            });
        }

        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            //SalesUserCustomerInfo.Data item = (SalesUserCustomerInfo.Data) getItem(position);
        }


        public static class Refresh {
        }

        public static void doRefresh() {
            EventBus.getDefault().post(new Refresh());
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void receiveRefresh(Refresh r) {
            onRefresh();
        }
    }
}